package org.shoukaiseki.common.validation;

import org.shoukaiseki.common.utils.StringUtils;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

/**
 *
 **/
public class ValidationUtils {

    //获取校验错误的提示消息,会带字段名
    public static String getValidationErrorMessage(ObjectError objectError){
        StringBuilder sb = new StringBuilder();
        if(objectError instanceof FieldError) {
            sb.append(((FieldError) objectError).getField()).append(":");
            if(StringUtils.isNotEmpty(((FieldError) objectError).getDefaultMessage())){
                sb.append(((FieldError) objectError).getDefaultMessage());
            }
//            ((FieldError) objectError).getObjectName();
            sb.append(";");
        }else{

            sb.append(objectError.getArguments() ).append(":");
            if(StringUtils.isNotEmpty(objectError.getDefaultMessage())){
                sb.append(objectError.getDefaultMessage());
            }
            sb.append(";");
        }
        return sb.toString();
    }

}
